//
//  GYHNetWorkTool.swift
//  sinaWeibo_18
//
//  Created by 高勇华 on 15/10/7.
//  Copyright © 2015年 高勇华. All rights reserved.
//

import UIKit
import AFNetworking
//定义一个枚举
enum HTTPMethod:String{
    
 case GET = "GET"
 case POST = "POST"

}


class GYHNetWorkTool: AFHTTPSessionManager {
    
    let WB_client_id = "4148423446"
    let WB_redirect_uri = "http://www.baidu.com"
    let WB_client_secret = "8fefec782c90a89a0b5650a8b5b7281f"

    
    //创建一个单列
    static let sharedTools :GYHNetWorkTool = {
        
    let tools = GYHNetWorkTool()
    tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
    return tools
        
    }()
    //加载oauth的url数据
    func oauthUrl() ->NSURL{
    let pageURL = "https://api.weibo.com/oauth2/authorize"
    let urlString = pageURL + "?" + "client_id=" + WB_client_id + "&" + "redirect_uri=" + WB_redirect_uri
    
        return NSURL(string: urlString)!
        
    }
  
    //获取Token值
    func loadAccessToken(code:String,completion:(dict:[String : AnyObject]?,error:NSError?)->()){
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let parame = ["client_id":WB_client_id,"client_secret":WB_client_secret,"grant_type":"authorization_code","code":code,"redirect_uri":WB_redirect_uri]
        
        
        requestJSON(HTTPMethod.POST, parames: parame, urlString: urlString, completion: completion)
        
        
       // print(parame)
   //测试代码 查看数据类型
        
        /*"access_token":"2.00e3A8NEa83kWEff1d2c47a95nMl7B",
        "remind_in":"157679999",
        "expires_in":157679999,
        "uid":"3863059956"*/
//        responseSerializer = AFHTTPResponseSerializer()
//        POST(urlString, parameters: parame, success: { (_, result) -> Void in
//            
//            let dataString = NSString(data: result as! NSData, encoding: NSUTF8StringEncoding)
//            
//            print(dataString)
//            
//
//            }) { (_, error) -> Void in
//                
//                
//                print(error)
//        }
//
        
//               POST(urlString, parameters: parame, success: { (_, result) -> Void in
////            print(result)
//                
//               if  let info = result as? [String : AnyObject]{
//                
//                 completion(dict: info, error: nil)
//                
//                            }else{
//                
//                completion(dict: nil, error: NSError(domain: "www.errorbaitu.com", code: -1022, userInfo: ["error":"数据不合法"]))
//                
//                }
//                
//            }) { (_, error) -> Void in
//                
//                completion(dict: nil, error: error)
//                
//             print(error)
//        }
//
   
    }
    
    
    func loadUserInfo(access_token:String,uid:String,completion:(dict:[String : AnyObject]?,error:NSError?)->()){
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        
       let parames = ["access_token":access_token,"uid":uid]
        
        requestJSON(HTTPMethod.GET, parames: parames, urlString: urlString, completion: completion)
        
//        GET(urlString, parameters: parames, success: { (_, result) -> Void in
////            print(result)
//            if let info = result as? [String : AnyObject]{
//                
//            completion(dict: info, error: nil)
//            print(info)
//            
//            }else{
//            completion(dict: nil, error: NSError(domain: "www.errorbaitu.com", code: -1022, userInfo: ["error":"数据不合法"]))
//            
//            }
//            
//            }) { (_, error) -> Void in
//                completion(dict: nil, error: error)
//                print(error)
//                
//                
//        }
        
        
    }
    
    func requestJSON(method:HTTPMethod,parames:[String : AnyObject]? = nil,urlString:String,completion:(dict:[String : AnyObject]?,error:NSError?)->()){
        
        /*
        GET(URLString: String, parameters: AnyObject?, success: ((NSURLSessionDataTask, AnyObject) -> Void)?, failure: ((NSURLSessionDataTask, NSError) -> Void)?) -> NSURLSessionDataTask?
        */
        
        let successCallBack = {(task:NSURLSessionDataTask,result:AnyObject) ->() in
        
            if let info = result as? [String : AnyObject]{
                
                completion(dict: info, error: nil)
                print(info)
                
            }else{
                completion(dict: nil, error: NSError(domain: "www.errorbaitu.com", code: -1022, userInfo: ["error":"数据不合法"]))
                
            }
 
            
        
        }
        
        
        let failureCallBack = {(task:NSURLSessionDataTask,error:NSError) ->() in
            
            completion(dict: nil, error: error)
             print(error)
           
            
        
        }
        
        
        
    
        if method == HTTPMethod.GET {
        
            GET(urlString, parameters: parames, success:successCallBack , failure: failureCallBack)
            

            
//            GET(urlString, parameters: parames, success: { (_, result) -> Void in
                //            print(result)
                
//                if let info = result as? [String : AnyObject]{
//                    
//                    completion(dict: info, error: nil)
//                    print(info)
//                    
//                }else{
//                    completion(dict: nil, error: NSError(domain: "www.errorbaitu.com", code: -1022, userInfo: ["error":"数据不合法"]))
                
                
                
              //  }
                
                
                
//                }) { (_, error) -> Void in
//                    completion(dict: nil, error: error)
//                    print(error)
//                    
//                    
//            }
 
            
            
        
        }else{
            
            POST(urlString, parameters: parames, success: successCallBack, failure: failureCallBack)
        
//            POST(urlString, parameters: parames, success: { (_, result) -> Void in
//                //            print(result)
//                
//                if  let info = result as? [String : AnyObject]{
//                    
//                    completion(dict: info, error: nil)
//                    
//                }else{
//                    
//                    completion(dict: nil, error: NSError(domain: "www.errorbaitu.com", code: -1022, userInfo: ["error":"数据不合法"]))
//                    
//                }
//                
//                }) { (_, error) -> Void in
//                    
//                    completion(dict: nil, error: error)
//                    
//                    print(error)
//            }
            

        
        
        }
    
    
    
    }
    
    
    
    
}
